package com.syxx.oa.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.syxx.oa.domain.Department;
import com.syxx.oa.service.DepartMentService;
import com.syxx.oa.utils.StringUtils;
import com.test.jbpm.Pager;
import com.test.jbpm.web.action.BaseAction;

@Controller
@Scope("prototype")
public class DepartMentAcion extends BaseAction{
	
	private static final long serialVersionUID = 1L;

	private DepartMentService departMentService;
	
	private Department dep;
	
	private Long[] depIds;
	
	public Department getDep() {
		return dep;
	}

	public void setDep(Department dep) {
		this.dep = dep;
	}

	

	public Long[] getDepIds() {
		return depIds;
	}

	public void setDepIds(Long[] depIds) {
		this.depIds = depIds;
	}

	@Autowired
	public void setDepartMentService(DepartMentService departMentService) {
		this.departMentService = departMentService;
	}
	
	public String addOrUpdate(){
		String retStr = "";
		try {
			if(dep.getParent() == null || dep.getParent().getDepId()==null || dep.getParent().getDepId()==0L){
				dep.setParent(null);
			}
			departMentService.addOrUpdate(dep);
			retStr = ajaxOut("{success: true}");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retStr;
	}
	
	public String index() throws Exception {
		return SUCCESS;
	}
	
	public String list() throws Exception {
		Pager<Department> pager = departMentService.findDepList();
		StringBuffer sbJson = new StringBuffer();
		sbJson.append("{");
		sbJson.append("total:");
		sbJson.append(pager.getTotal());
		sbJson.append(", data:");
		List<Department> userList = pager.getData();
		Map<String, Object> map;
		JSONObject jsonObject;
		JSONArray list = new JSONArray();
		for (Department dep : userList) {
			map = new HashMap<String, Object>();
			if(dep.getEmployees() != null){
				dep.setEmployees(null);
			}
			if(dep.getParent() != null){
				map.put("parentId", dep.getParent().getDepId());
				map.put("parentName", dep.getParent().getDepName());
				dep.setParent(null);
			}
			jsonObject = JSONObject.fromObject(dep);
			if(!map.isEmpty()){
				jsonObject.putAll(map);
			}
			list.add(jsonObject);
		}
		sbJson.append(JSONArray.fromObject(list));
		sbJson.append("}");
        return ajaxOut(sbJson.toString());
	}
	
	public String edit() throws Exception {
		dep = departMentService.findDepById(dep.getDepId());
		dep.setEmployees(null);
		Map<String,Object> map = new HashMap<String, Object>();
		if(dep.getParent() != null && dep.getParent().getDepId() != null && dep.getParent().getDepId() != 0){
			Long depId=dep.getParent().getDepId();
			map.put("parentId", depId);
		}else{
			map.put("parentId", "");
		}
		
		dep.setParent(null);
		JSONObject jsonObject = JSONObject.fromObject(dep);
		jsonObject.putAll(map);
		
		//测试
		System.out.println("department json:"+jsonObject);
		return ajaxOut("{success:true, data:" + jsonObject + "}");
	}
	
	public String del() throws Exception{
		departMentService.del(depIds);
		return ajaxOut();
	}
	
	public String findList() throws Exception{
		List<Department> reList = departMentService.findDepList(null);
		for (Department dep : reList) {
			if(dep.getEmployees() != null){
				dep.setEmployees(null);
			}
		}
		return ajaxOut(StringUtils.list2Json(reList).toString());
	}
	
	
}
